/**
 * 自定义函数
 * */


//列表
function getIndex(){
    var uinfo = getAdminInfo();
    // var keyword = $('#keyword').val();
    var pageStart = getParamByUrl('start');
    pageStart = pageStart ? pageStart : pages.start;
    // var sort_id = $('#sort_id').val();
    var formData = {
        role_id: uinfo.role_id, depart_id: uinfo.depart_id,
        kword: keyword,
        sort_id: sort_id,
        start: pageStart, limit: pages.limit,
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'config/wh/index',
        data: formData,
        success: function (res) {
            console.log(res);
            var h = '', tbody = $('tbody');
            if (res.code < 0) {
                h = '<tr class="text-c">';
                h += '<td colspan="10">没有记录</td>';
                h += '</tr>';
                tbody.html(h);
                return
            }
            tbody.html(getHtml(res.data));
            $('#pageTotal').html(res.total);
            pages.start = pageStart;
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
//列表字符串
function getHtml(d){
    var uinfo = getAdminInfo();
    var xuhao, pageStart = getParamByUrl('start');
    pageStart = pageStart ? pageStart : pages.start;
    var h = '', i, ii, cnts;
    h += '';
    for (i = 0; i < d.length; i ++) {
        xuhao = (pageStart - 1) * pages.limit + i + 1;
        h += '<tr class="text-c">';
        h += '<td title="' + d[i].id + '">' + xuhao + '</td>';
        h += '<td>' + d[i].title + '</td>';
        h += '<td>';
        cnts = d[i].cnts;
        if (uinfo.role_id != 2) {
            h += '<a href="javascript:;" style="color: #0088CC;" onclick="lay_cnt_edit(\'添加内容\',\'wh-cnt.html?wh_id=' + d[i].id + '\', 0);">新增内容</a>';
        }
        for (ii = 0; ii < cnts.length; ii ++) {
            h += '<div>';
            h += cnts[ii].cnt;
            h += '<input type="hidden" id="cnt' + cnts[ii].id + '" value="' + cnts[ii].cnt + '"/>';
            if (uinfo.role_id != 2) {
                h += '<a href="javascript:;" style="padding: 0 10px; color: #0088CC;" onclick="lay_cnt_edit(\'修改内容\',\'wh-cnt.html?wh_id=' + d[i].id + '\', ' + cnts[ii].id + ');">修改</a>';
                h += '<a href="javascript:;" style="color: red;" onclick="lay_cnt_del(' + cnts[ii].id + ');">删除</a>';
            }
            h += '</div>';
        }
        h += '</td>';
        h += '<td width="70">' + d[i].lasttime + '</td>';
        if (uinfo.role_id != 2) {
            h += '<td class="td-manage">';
            h += '<a href="javascript:;" class="ml-5" onclick="lay_edit(\'编辑\',\'wh-edit.html\', ' + d[i].id + ');">';
            h += '编辑';
            h += '<i class="Hui-iconfont">&#xe6df;</i>';
            h += '</a>';
            h += '&nbsp;&nbsp;';
            h += '<a href="javascript:;" class="ml-5" onclick="lay_del(' + d[i].id + ');" style="color: red;">';
            h += ' 删除';
            h += '<i class="Hui-iconfont"></i>';
            h += '</a>';
            h += '<input type="hidden" id="title' + d[i].id +'" value="' + d[i].title + '"/>';
            h += '</td>';
        }
        h += '</tr>';
    }
    return h;
}


function toJump(){
    var keyword = $('#keyword').val();
    var sort_id = $('#sort_id').val();
    var p = '?keyword=' + escape(keyword);
    p += '&sort_id=' + sort_id;
    window.location.href = 'workhour.html' + p;
}


function getOption(){
    var h = '';
    // 搜索词
    $('#keyword')[0].value = keyword;
    // 排序
    h = '';
    if (sort_id == 2) {
        h += '<option value="1">按时间正序</option>';
        h += '<option value="2" selected>按时间倒序</option>';
    } else {
        h += '<option value="1" selected>按时间正序</option>';
        h += '<option value="2">按时间倒序</option>';
    }
    $('#sort_id').html(h);
}


//编辑弹窗
function lay_edit(title,url,id,w,h){
    url += '?id=' + id;
    var tt = $('#title' + id).val();
    tt = tt ? escape(tt) : '';
    url += '&title=' + tt;
    h = 300;
    layer_show(title,url,w,h);
}


//获取详情
function getOne(){
    if (!id) { return }
    $('#title')[0].value = title;
}


////显示图片
//function showImg(e){
//    var h = '';
//    h += '<img src="' + e.img + '" style="width: 150px;"/>';
//    h += '<input type="hidden" id="img" value="' + e.img + '"/>';
//    $('#bigShow').html(h);
//}


//修改
function modify(){
    var uinfo = getAdminInfo();
    var formData = {
        id: id, title: $('#title').val(), detail: $('#detail').val(),
        staff_id: uinfo.uid, role_id: uinfo.role_id, depart_id: uinfo.depart_id,
    };
    if (formData.title == '') {
        msgPop({msg: '名称必须的'}); return
    }
    console.log(formData); //return
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'config/wh/modify',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                //msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '操作成功'});
            //window.location.href = '';
            setTimeout(function(){
                top.location.reload();
            }, 500);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//删除
function lay_del(id) {
    var msg = '确认要 <b style="color: red; font-size: 20px;">删除</b> 吗？';
    layer.confirm(msg, function (index) {
        $.ajax({
            type: apiMethod,
            url: apiDomain + 'config/wh/del',
            data: {  id: id },
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    msgPop({msg: res.msg});
                    return
                }
                layer.msg('已删除!', {icon: 1, time: 1000});
                setTimeout(function () {
                    //top.location.reload();
                    window.location.reload();
                }, 500);
            },
            error: function () {
                console.log('错误了！');
            }
        });
    });
}


//内容弹窗
function lay_cnt_edit(title,url,id,w,h){
    url += '&id=' + id;
    var tt = $('#cnt' + id).val();
    tt = tt ? escape(tt) : '';
    url += '&cnt=' + tt;
    h = 300;
    layer_show(title,url,w,h);
}


//获取内容
function cntOne(){
    $('#cnt').html(cnt);
}


//修改
function cntModify(){
    var cnt = $('#cnt').val();
    var formData = {
        id: id, cnt: cnt, wh_id: wh_id,
    };
    if (formData.detail == '') {
        msgPop({msg: '内容必须的'}); return
    }
    console.log(formData); //return
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'config/wh/cnt/modify',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                //msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '操作成功'});
            //window.location.href = '';
            setTimeout(function(){
                top.location.reload();
            }, 500);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//删除
function lay_cnt_del(id) {
    var msg = '确认要 <b style="color: red; font-size: 20px;">删除</b> 吗？';
    layer.confirm(msg, function (index) {
        $.ajax({
            type: apiMethod,
            url: apiDomain + 'config/wh/cnt/del',
            data: {  id: id },
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    msgPop({msg: res.msg});
                    return
                }
                layer.msg('已删除!', {icon: 1, time: 1000});
                setTimeout(function () {
                    //top.location.reload();
                    window.location.reload();
                }, 500);
            },
            error: function () {
                console.log('错误了！');
            }
        });
    });
}